package ru.dcsearch.indexer;
 /***************************************************************************
 *   Modified by TMC
 *   berechitai@gmail.com
 *
 *   This file is part of DC Indexer project
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 ***************************************************************************/
/*
 *  Mobile DC - a DirectConnect client written in Java and designed for
 *  the Sony Ericsson p800/p900 or equivalent Symbian OS 7.0 platforms
 *  Copyright (C) 2004 Conrad Helgesson, vakum@home.se
 *
 *  Mobile DC has been developed with the help of SymIRC 0.43 as guidence.
 *  Great influence and motivation has been taken from this IRC klient
 *  by Tommy Tonteri, akiro@mureakuha.com and Rupert Hanson, wolvie@wolvie.org
 *  Big thanks!
 *
 *  This file is part of Mobile DC.
 *
 *  Mobile DC is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Mobile DC is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Mobile DC; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.Socket;
import java.net.UnknownHostException;

// Connect and Disconnect-class.
public class NetHandler extends Thread
{

	public boolean				isConnected;
	public Socket				s = null;
	private TokenInputStream    in;
	private DCClient			mw;

	final static char COMMAND_END_CHAR='|'; // endchar in protocol
	final static char COMMAND_SEP_CHAR=' '; // separator in protocol

	public NetHandler(DCClient w, boolean recon)
	{
		mw = w;
		isConnected = recon;
	}

	//Connector Thread
	public void run()
	{
        while (true) {
            Logger.printLog("STARTUP: connector");


            if(!isConnected) {
                isConnected = true;
            }

            try { // establish connection
                s = new Socket(mw.p.getMyhub(), Integer.parseInt(mw.p.getMyport()));
                in = new TokenInputStream(new BufferedInputStream(s.getInputStream()), COMMAND_END_CHAR);
            } catch (UnknownHostException e) {
                disconnect("*** Unknown address");
            } catch (InterruptedIOException e) {
                disconnect("*** Connection timeout");
            } catch (IOException e) {
                disconnect("*** Connection refused by target machine");
            } catch (Exception e) {
                disconnect("*** "+e);
            }

            if(isConnected) {
                mw.nw = new NetWorker(mw);
                mw.nw.start();
                Logger.printGlobalLog("Connected");
            }



            do  {
               try {
                    Thread.sleep(3000);

               } catch (Exception e) {
                   
               }

               if (s!=null && s.isClosed()) {

                        disconnect("Lost connection");

               }
            }   while (isConnected);
        }
	}

	// disconnect
	synchronized public void disconnect(String reason)
	{

		if(!isConnected && reason.equals("*** Disconnected")) {
			Logger.printLog("*** Not Connected");
		} else if(isConnected) {


			if(reason.length() == 0) Logger.printGlobalLog("*** Disconnected");
	    	else Logger.printLog(reason);
	    	Logger.printGlobalLog("ACTION: "+reason+"...");

	    	mw.ul.ClearData(true);

			try {
	    		s.close();
	    		Logger.printLog("ACTION: s.close()...");
	        	s = null;
	        	Logger.printLog("ACTION: s = null...");
	        	mw.nw.stopThread();
	        	Logger.printLog("ACTION: nw.stop()...");
	        } catch (Exception e) {
	        }

            isConnected = false;
		}

	}

    public  TokenInputStream getIn() {

        return in;

    }

}
